package com.ruoyi.web.controller.laiqianba.merchant;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.*;
import com.ruoyi.merchant.service.IMerchantBaseInfoService;
import com.ruoyi.merchantdata.service.IMerchantDataAllocateInfoService;
import com.ruoyi.merchantdata.service.IMerchantUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.merchant.service.IMerchantDataPushInfoService;
import com.ruoyi.web.wrapper.MerchantDataPushInfoWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户推送信息Controller
 *
 * @author ruoyi
 * @date 2023-09-05
 */
@Slf4j
@RestController
@RequestMapping("/merchant/pushInfo")
public class MerchantDataPushInfoController extends BaseController {
    @Autowired
    private IMerchantDataPushInfoService merchantDataPushInfoService;

    @Autowired
    private IMerchantBaseInfoService merchantBaseInfoService;

    @Autowired
    private IMerchantDataAllocateInfoService merchantDataAllocateInfoService;

    @Autowired
    private IMerchantUserInfoService merchantUserInfoService;


    /**
     * 查询商户推送信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantDataPushInfo merchantDataPushInfo) {

        log.info("【数据请求123123】【uid】{}【merchant】{}", JSON.toJSONString(SecurityUtils.getLoginUser()), JSON.toJSONString(merchantDataPushInfo));

        startPage();
        if(StrUtil.equals(merchantDataPushInfo.getChannelName(),"undefined") ||StrUtil.equals(merchantDataPushInfo.getChannelName(),"null")){
            merchantDataPushInfo.setChannelName("");
        }
        List<Map<String, Object>> list = merchantDataPushInfoService.selectMerchantDataPushInfoMapList(merchantDataPushInfo);
        list = new MerchantDataPushInfoWrapper(list).wrap();
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('merchant:pushInfo1:list')")
    @GetMapping("/list1")
    public TableDataInfo listTuiSong(MerchantDataPushInfo merchantDataPushInfo) {

        if (!SecurityUtils.loginUserIsAdmin()) {
            Long deptId = SecurityUtils.getDeptId();
            Long InfoId = merchantBaseInfoService.seletInfoIdBydeptId(deptId);
            merchantDataPushInfo.setInfoId(InfoId);
        }
        startPage();
        List<Map<String, Object>> list = merchantDataPushInfoService.selectMerchantDataPushInfoMapList1(merchantDataPushInfo);
        return getDataTable(list);
    }

    /**
     * 查询折线图列表
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:count')")
    @GetMapping("/count/list")
    public AjaxResult count(MerchantDataPushInfo merchantDataPushInfo) {
        List<Map<String, Object>> list = merchantDataPushInfoService.selectMerchantDataList(merchantDataPushInfo);
        JSONObject returnData = new JSONObject();
        returnData.put("dataList", list);
        return success(returnData);
    }

    /**
     * 导出商户推送信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:export')")
    @Log(title = "商户推送信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MerchantDataPushInfo merchantDataPushInfo) {
        List<MerchantDataPushInfo> list = merchantDataPushInfoService.selectMerchantDataPushInfoList(merchantDataPushInfo);
        ExcelUtil<MerchantDataPushInfo> util = new ExcelUtil<MerchantDataPushInfo>(MerchantDataPushInfo.class);
        util.exportExcel(response, list, "商户推送信息数据");
    }

    /**
     * 获取商户推送信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        MerchantDataPushInfo merchantDataPushInfo = merchantDataPushInfoService.selectMerchantDataPushInfoById(id);
        merchantDataPushInfo.setParamInfo("");
        merchantDataPushInfo.setResultInfo("");
        return success(merchantDataPushInfo);
    }

    /**
     * 新增商户推送信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:add')")
    @Log(title = "商户推送信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantDataPushInfo merchantDataPushInfo) {
        return toAjax(merchantDataPushInfoService.insertMerchantDataPushInfo(merchantDataPushInfo));
    }

    /**
     * 修改商户推送信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:edit')")
    @Log(title = "商户推送信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantDataPushInfo merchantDataPushInfo) {
        return toAjax(merchantDataPushInfoService.updateMerchantDataPushInfo(merchantDataPushInfo));
    }

    /**
     * 删除商户推送信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:remove')")
    @Log(title = "商户推送信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(merchantDataPushInfoService.deleteMerchantDataPushInfoByIds(ids));
    }


    /**
     * 用户捞取接口
     */
    @PreAuthorize("@ss.hasPermi('merchant:pushInfo:laoqu')")
    @Log(title = "用户捞取接口", businessType = BusinessType.UPDATE)
    @PostMapping("/laoqu")
    public AjaxResult laoqu(@RequestBody MerchantDataPushInfo merchantDataPushInfo) {

        return toAjax(merchantDataPushInfoService.laoquMerchantDataPoolInfo(merchantDataPushInfo));
    }


    @PostMapping("/getRegisterException")
    public AjaxResult getRegisterException(@RequestBody JSONObject jsonObject) {

        return toAjax(merchantDataPushInfoService.getRegisterException(jsonObject));
    }

}
